package br.com.zynger.mst.addressbook;

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

//Refactoring 6: Extract superclass
public class PersonForm extends FormFrame {

	private static final long serialVersionUID = 1L;
	
	private JTextField firstNameTextField;
	private JTextField lastNameTextField;
	private JTextField addressTextField;
	private JTextField cityTextField;
	private JTextField stateTextField;
	private JTextField zipCodeTextField;
	private JTextField phoneNumberTextField;
	
	//Refactoring 5: Extract class
	public PersonForm() {
		init();
		
		setTitle("Add person to address book");
	}
	
	public PersonForm(Person p) {
		init();
		
		setTitle("Edit " + p.getFirstName() + " " + p.getLastName());
		firstNameTextField.setText(p.getFirstName());
		firstNameTextField.setEnabled(false);
		lastNameTextField.setText(p.getLastName());
		lastNameTextField.setEnabled(false);
		addressTextField.setText(p.getAddress());
		cityTextField.setText(p.getCity());
		stateTextField.setText(p.getState());
		zipCodeTextField.setText(p.getZipCode());
		phoneNumberTextField.setText(p.getPhoneNumber());
	}

	private void init() {
		super.init("", 400, 300);
		
		JPanel panel = new JPanel(new SpringLayout());
		firstNameTextField = createFormField(panel, "First Name:");
		lastNameTextField = createFormField(panel, "Last Name:");
		addressTextField = createFormField(panel, "Address:");
		cityTextField = createFormField(panel, "City:");
		stateTextField = createFormField(panel, "State:");
		zipCodeTextField = createFormField(panel, "Zip Code:");
		phoneNumberTextField = createFormField(panel, "Phone:");
		
		addFormGrid(panel, 7);
	}
	
	public String getFirstNameTextFieldValue(){
		return firstNameTextField.getText().toString().trim();
	}
	
	public String getLastNameTextFieldValue(){
		return lastNameTextField.getText().toString().trim();
	}

	public String getAddressTextFieldValue(){
		return addressTextField.getText().toString().trim();
	}
	
	public String getCityTextFieldValue(){
		return cityTextField.getText().toString().trim();
	}
	
	public String getStateTextFieldValue(){
		return stateTextField.getText().toString().trim();
	}
	
	public String getZipCodeTextFieldValue(){
		return zipCodeTextField.getText().toString().trim();
	}
	
	public String getPhoneNumberTextFieldValue(){
		return phoneNumberTextField.getText().toString().trim();
	}
}
